package com.his.controller;

import com.his.annotation.EnableEncryption;
import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Patient;
import com.his.service.PatientService;
import com.his.utils.JWTUtil;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * (Patient)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:14:21
 */
@Validated
@RestController
@RequestMapping("patient")
public class PatientController {
    /**
     * 服务对象
     */
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Rsp selectOne(@NotNull(message = "病人id不能为空") Integer id, @RequestHeader(Const.TOKEN) String token) {
        return Rsp.success(this.patientService.queryById(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param token 前端传递的令牌
     * @return 单条数据
     */
    @GetMapping("getAll")
    public Rsp getAll(@RequestHeader(Const.TOKEN) String token) {
        return Rsp.success(this.patientService.queryAll(new Patient()));
    }

    @RequireRoles(roles = {"doctor", "nurse", "hr", "charge"}, index = 1)
    @GetMapping("getByPhone")
    public Rsp getByPhone(@NotNull(message = "病人电话号码不能为空") String tellPhone, @RequestHeader(Const.TOKEN) String token) {
        Patient patient = new Patient();
        patient.setTellphone(tellPhone);
        return Rsp.success(this.patientService.queryAll(patient).get(0));
    }

    /**
     * 病人登录的方法
     *
     * @param patient 登录的用户
     * @return 登录是否成功
     */
    @PostMapping("login")
    public Rsp login(@RequestBody Patient patient) throws Exception {
        Patient login = patientService.login(patient);
        if (login == null) {
            return Rsp.fail().msg("用户名或密码错误");
        }
        return Rsp.success(null).token(JWTUtil.generateToken(login, 30, Const.PRIVATE_KEY));
    }

    @PostMapping("register")
    public Rsp register(@Validated @RequestBody Patient patient) {
        return patientService.insert(patient);
    }

    @EnableEncryption
    @RequireRoles(roles = {"doctor", "admin","hr"}, index = 0)
    @PostMapping("searchSomePatientPage")
    public Rsp searchSomePatientPage(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Patient> pageParam) throws Exception {
        return Rsp.success(this.patientService.searchSomePatientPage(pageParam, MyUtil.getUser(token)));
    }

    /**
     * 修改病人信息的方法
     *
     * @param token   令牌参数
     * @param patient 病人信息
     * @return 返回修改状态
     */
    @RequireRoles(roles = {"hr"}, index = 0)
    @PostMapping("updatePatient")
    public Rsp updatePatent(@RequestHeader(Const.TOKEN) String token,
                            @Validated({Const.Update.class}) @RequestBody Patient patient) {
        return this.patientService.update(patient) ? Rsp.success(null) : Rsp.fail().msg("修改失败");
    }

    /**
     * patientList
     * 获取所有病人列表
     */
    @PostMapping("selectPatientList")
    public Rsp selectPatientList() {
        return patientService.selectPatientList();
    }
}
